const gulp=require('gulp');

const gulpJshint=require('gulp-jshint');//检查代码 
const gulpLess=require('gulp-less');//less
const gulpConcat =require('gulp-concat');//整合
const gulpUglify=require('gulp-uglify');//js压缩
const gulpRename=require('gulp-rename');//启用别名
const connect = require('gulp-connect'); // 起server服务
const htmlmin = require('gulp-htmlmin'); // html压缩
const del = require('del'); // 清空目录


gulp.task('clean', async () => {
    console.log("clean")
    await del(['./dist'])
});

gulp.task('lint',async()=>{
    console.log("lint")
    await gulp.src('src/js/*.js')//该任务针对的文件
    .pipe(gulpJshint())//该任务调用的模块
    .pipe(gulpJshint.reporter('default'))
})

gulp.task('less',async()=>{
    console.log("less")
    await gulp.src('src/less/*.less')
    .pipe(gulpLess())
    .pipe(gulp.dest('./dist/src/css'))
})

gulp.task('scripts',async()=>{
    console.log("scripts")
    await gulp.src('src/js/*.js')
    .pipe(gulpConcat('all.js'))
    .pipe(gulp.dest('./dist'))
    .pipe(gulpRename('all.min.js'))
    .pipe(gulpUglify())
    .pipe(gulp.dest('./dist/src/js'))
});

gulp.task('watch',function(){
    console.log("watch")
    gulp.watch(['src/less/*.less'], gulp.series(gulp.parallel('lint', 'less', 'scripts')));
    gulp.watch('src/js/*.js', gulp.series(gulp.parallel('lint', 'less', 'scripts')));
})

gulp.task('server', () => {
    connect.server(
        {
            root: './dist',
            port: 8080,
            livereload: true
        }
    )
});
gulp.task('html', () => {
    return gulp.src('./index.html')
        .pipe(htmlmin({
            collapseInlineTagWhitespace: true,
        }))
        .pipe(gulp.dest('dist'))
});

gulp.task('default', gulp.series('clean',gulp.parallel('lint', 'less', 'scripts','html','watch','server')))

